package
{
	import asf.controllers.FrontController;
	import asf.core.Application;
	import asf.sections.Section;
	import asf.transitions.BasicFadeTransition;
	import asf.utils.FX;
	
	import br.pedromoraes.btween.ISequenceable;
	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import sample.app.Commands;
	import sample.app.Globals;
	import sample.cmdparams.NavigateCommandParams;
	import sample.commands.NavigateCommand;
	import sample.view.assets.MainLogo;
	import sample.viewassets.global.MainMenu;
	import sample.viewassets.pops.InternalPopup;

	public class Main extends Sprite
	{
		private var app:Application;
		
		private var logo:MainLogo;
		private var menu:MainMenu;
		
		public function Main( )
		{
			super( );
			
			InternalPopup;
		}
		
		public function init( ):void
		{
			app = Application.getByID( Globals.APP_ID );
			app.navigation.defaultTransition = new BasicFadeTransition( );
			
			ctrace( app );
			
			render( );
			intro( ).queue( initCommands );
		}
		
		private function initCommands( ):void
		{
			FrontController.pushCommand( Commands.NAVIGATE, new NavigateCommand( ) );
		}
		
		private function render( ):void
		{
			logo = new MainLogo( );
			menu = new MainMenu( );
			
			logo.alpha = 0;
			
			app.layers.logo.addChild( logo );
			app.layers.menu.addChild( menu );
			
			menu.addEventListener( Event.SELECT, menuSelectHandler );
		}
		
		private function intro( ):ISequenceable
		{
			return FX.fadeIn( logo ).queue( menu.show );
		}
		
		private function menuSelectHandler( evt:Event ):void
		{
			var section:Section = menu.getSelectedSection( );
			
			FrontController.broadcastEvent( Commands.NAVIGATE, new NavigateCommandParams( section.id ) );
		}
	}
}